package com.hanyi.mvc.contorller;

import com.hanyi.mvc.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
public class UserDemoContorller {

    @RequestMapping("/login")
    public String login(User user, HttpSession session, HttpServletRequest request){
        System.out.println("loginUser = " + user);
        if (Objects.equals("admin",user.getUsername())&& Objects.equals("admin",user.getPassword())){
           session.setAttribute("user",user);
           return "redirect:/index.jsp";
        }
        request.setAttribute("msg","账号或,密码输入错误");
        return "forward:/login.jsp";
    }

    @RequestMapping("/select")
    public String selectAll(){
        return "forward:/WEB-INF/list.html";
    }

    @RequestMapping("/users")
    @ResponseBody
    public  List<User>  list(){
        List<User> users = new ArrayList<>();
        users.add(new User("tom",23,"男","123"));
        users.add(new User("jerry",23,"男","123"));
        users.add(new User("rose",23,"女","123"));
        users.add(new User("jack",23,"男","123"));
        return users;
    }
}
